package com.hmyd.hmyd_android.ui;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.BankCard;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 银行卡列表
 * @author Administrator
 *
 */
@ContentView(R.layout.activity_bank)
public class BankActivity extends BasicActivity {
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;
	
	private final int BANKCARD = 1;

	@ViewInject(R.id.bank_info)
	private View bank_info;
	@ViewInject(R.id.bank_logo)
	private ImageView bank_logo;
	@ViewInject(R.id.bank_name)
	private TextView bank_name;
	@ViewInject(R.id.bank_id)
	private TextView bank_id;
	@ViewInject(R.id.bank_tel)
	private TextView bank_tel;
	@ViewInject(R.id.recharge_now_btn)
	private Button recharge_now_btn; // 立即充值按钮
	
	private static String BANK_CARD = "bank_card";
	
	BankCard bankCard;
	
	/*public static Intent getIntent(Context context,BankCard bankCard){
		Intent intent = new Intent(context, BankActivity.class);
		Bundle mBundle = new Bundle();  
        mBundle.putSerializable(CARD_KEY, bankCard);
		intent.putExtras(mBundle);
		
		return intent;
	}*/
	
	private void onRequest(){
		if (null != bankCard) {
			setBindCard(bankCard);
		} else {
			onRequest(BANKCARD, UrlConstant.BANKCARD, HttpMethod.POST, null);
		}
	};
	
	@OnClick({R.id.recharge_now_btn})
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.recharge_now_btn:
			Intent intent = new Intent(this,RechargeActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}
	
	@Override
	public void onPostSuccess(int postId, String msg) {
		Gson gson = new Gson();
		switch (postId) {
		case BANKCARD:
			java.lang.reflect.Type type2 = new TypeToken<BankCard>() {}.getType();
			BankCard bankCard = gson.fromJson(msg, type2);
			setBindCard(bankCard);
			break;

		default:
			break;
		}
	}

	@Override
	public void onPostFailure(int postId, String msg) {
		switch (postId) {
		case BANKCARD:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected void setParams() {
		home_title_txt.setText(getString(R.string.assets_bank_card));
		home_title.setOnTouchListener(touch);
		
		bankCard = (BankCard)getIntent().getSerializableExtra(BANK_CARD);
		
		onRequest();
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}
	
	private void setBindCard(BankCard bankCard){
		if (bankCard != null) {
			bank_info.setVisibility(View.VISIBLE);
		} else {
			bank_info.setVisibility(View.INVISIBLE);
		}
		
		if(bankCard.code==1){//已经绑卡成功，显示信息
			
			if(bankCard.data.bankCode.equals("BOC")){
				bank_logo.setImageResource(R.drawable.yin_zhongguo);
			}else if(bankCard.data.bankCode.equals("ABC")){
				bank_logo.setImageResource(R.drawable.yin_nogn);
			}else if(bankCard.data.bankCode.equals("CCB")){
				bank_logo.setImageResource(R.drawable.yin_jian);
			}else if(bankCard.data.bankCode.equals("ICBC")){
				bank_logo.setImageResource(R.drawable.yin_gong);
			}else if(bankCard.data.bankCode.equals("CMB")){
				bank_logo.setImageResource(R.drawable.yin_zhao);
			}else if(bankCard.data.bankCode.equals("CITIC")){
				bank_logo.setImageResource(R.drawable.yin_zhong);
			}else if(bankCard.data.bankCode.equals("CMBC")){
				bank_logo.setImageResource(R.drawable.yin_min);
			}else if(bankCard.data.bankCode.equals("GDB")){
				bank_logo.setImageResource(R.drawable.yin_guangfa);
			}else if(bankCard.data.bankCode.equals("CIB")){
				bank_logo.setImageResource(R.drawable.yin_xing);
			}else if(bankCard.data.bankCode.equals("CEB")){
				bank_logo.setImageResource(R.drawable.yin_guagn);
			}else if(bankCard.data.bankCode.equals("BOS")){
				bank_logo.setImageResource(R.drawable.yin_shagn);
			}else if(bankCard.data.bankCode.equals("PSBC")){
				bank_logo.setImageResource(R.drawable.yin_you);
			}else if(bankCard.data.bankCode.equals("HXB")){
				bank_logo.setImageResource(R.drawable.yin_hua);
			}else if(bankCard.data.bankCode.equals("SZPAB")){
				bank_logo.setImageResource(R.drawable.yin_pin);
			}else if(bankCard.data.bankCode.equals("SPDB")){
				bank_logo.setImageResource(R.drawable.yin_pu);
			}else if(bankCard.data.bankCode.equals("BCM")){
				bank_logo.setImageResource(R.drawable.yin_jiaot);
			} else if (bankCard.data.bankCode.equals("COMM")) {
				bank_logo.setImageResource(R.drawable.yin_jiaot);
			} else {
				bank_logo.setVisibility(View.INVISIBLE);
				bank_name.setText(bankCard.data.cardName);
			}
			
			bank_id.setText(bankCard.data.bankAccountCode);
			bank_tel.setText("银行客服电话："+bankCard.data.cardTel);
			
		}else if(bankCard.code==2){//进行绑卡操作
			startActivity(BindBankCardActivity.getIntent(this, bankCard));
			AppBasic.getInstance().finishCurrentActivity();
		}
	}

}
